<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/24
 * Time: 18:28
 */
namespace app\admin\controller;
use app\admin\model\DiGui;
use app\admin\model\UpFile;
use think\Controller;
use think\Request;
use app\admin\model\AdminUser as AdminUsers;
use app\admin\model\Roles as Role;

class Adminuser extends Controller
{
    /**加载表格
     * @param Request $request
     * @return mixed
     */
    public function show_list(Request $request){
//        echo \request()->module();exit;
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('adminuser/del')
            ->addTableHeaderAdd('adminuser/show_add')
            ->addTableHeaderSousuo()
            ->addTableColumn('id','text','ID')
            ->addTableColumn('user','text','账号')
            ->addTableColumn('nickname','text','昵称')
            ->addTableColumn('role_id','text','角色')
            ->addTableColumn('phone','text','手机')
            ->addTableColumn('email','text','email')
            ->addTableColumn('portrait','img','头像')
            ->addTableColumn('pid','text','上级管理员')
            ->addTableColumn('status','switch','状态',['开启','禁用'],'adminuser/change')
            ->addTableRightHeader('操作')
            ->addTableRight('edit','adminuser/show_edit','修改')
            ->addTableRight('del','adminuser/del','删除')
            ->addTablePage('adminuser/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian');
        $limit = $request->param('limit',10);
        $data = AdminUsers::where('id','>',0);
        if($tiaojian){
            $data = $data->where('user|nickname','like',"%$tiaojian%");
        }
        $data = $data->order('id asc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            $value['portrait'] = getFilePath($value['portrait']);
            if($value['pid']==0){
                $value['pid'] = '最上级';
            }else{
                $pid = AdminUsers::where('id',$value['pid'])->findOrEmpty()->toArray();
                if($pid){
                    if($pid['nickname']){
                        $value['pid'] = $pid['nickname'];
                    }else{
                        $value['pid'] = $pid['user'];
                    }
                }
            }
            $trole = Role::where('id',$value['role_id'])->findOrEmpty()->toArray();
            if($trole){
                $value['role_id'] = $trole['role_name'];
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function change(Request $request){
        $id = $request->param('id');
        $User = AdminUsers::where('id',$id)->findOrEmpty()->toArray();
        if(!Role::isMyPid($User['role_id'])){
            return json_my_response('权限不足');
        }
        if($User['status']==1){
            $User['status'] = 0;
        }else{
            $User['status'] = 1;
        }
        if(AdminUsers::where('id',$User['id'])->update(['status'=>$User['status']])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**加载添加管理员
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        $user = session('adminuser');
        $digui = new DiGui();
        $roles = $digui->getData(session('modular').'_roles',$user['role_id' ]);
        if(!$roles){
            $roles = [];
        }
        $myrole = Role::where('id',$user['role_id'])->findOrEmpty()->toArray();
        $role = [];
        foreach ($roles as $value){
            $role[$value['id']] = $value['role_name'];
        }
        $role[$user['role_id']] = $myrole['role_name'];
        return NewBuilder::make('form')
            ->addAction('adminuser/add')
            ->addNone('pid',$user['id'])
            ->addInput('user','账号')
            ->addInput('nickname','昵称')
            ->addInput('pwd','密码')
            ->addInput('phone','手机')
            ->addInput('email','邮箱')
            ->addSwitch('status','状态',1)
            ->addImg('portrait','头像')
            ->addSelect('role_id','角色',null,$role,true)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加管理员
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $user = AdminUsers::where('user',$data['user'])->findOrEmpty()->toArray();
        if(!empty($user)){
            return redirect('index/errors',['code'=>205]);
        }
        if(!$data['pwd']){
            return redirect('index/errors',['code'=>205]);
        }
        $data['pwd'] = my_encryption($data['pwd'],true);
        if(AdminUsers::create($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**加载添加
     * @param Request $request
     * @return mixed
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $newuser = AdminUsers::where('id',$id)->findOrEmpty()->toArray();
        $newrole = Role::where('id',$newuser['role_id'])->findOrEmpty()->toArray();
        if(!Role::isMyPid($newrole['id'])){
            echo '权限不足';exit;
        }
        $user = session('adminuser');
        $digui = new DiGui();
        $roles = $digui->getData(session('modular').'_roles',$user['role_id' ]);
        if(!$roles){
            $roles = [];
        }
        $myrole = Role::where('id',$user['role_id'])->findOrEmpty()->toArray();
        $role = [];
        foreach ($roles as $value){
            $role[$value['id']] = $value['role_name'];
        }
        $role[$user['role_id']] = $myrole['role_name'];
        return NewBuilder::make('form')
            ->addAction('adminuser/edit')
            ->addNone('id',$id)
            ->addNone('pid',$newuser['pid'])
            ->addInput('user','账号',$newuser['user'])
            ->addInput('nickname','昵称',$newuser['nickname'])
            ->addInput('pwd','密码')
            ->addInput('phone','手机',$newuser['phone'])
            ->addInput('email','邮箱',$newuser['email'])
            ->addSwitch('status','状态',$newuser['status'])
            ->addImg('portrait','头像',$newuser['portrait'])
            ->addSelect('role_id','角色',$newuser['role_id'],$role,true)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**修改管理员
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $jiu = AdminUsers::where('id',$data['id'])->findOrEmpty()->toArray();
        if($jiu['user']!==$data['user']){
            $chong = AdminUsers::where('user',$data['user'])->findOrEmpty()->toArray();
            if(!empty($chong)){
                return redirect('index/errors',['code'=>205]);
            }
        }
        if($data['pwd']){
            $data['pwd'] = my_encryption($data['pwd'],true);
        }else{
            unset($data['pwd']);
        }
        if(AdminUsers::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function del(Request $request){
        $id = $request->param('id');
        $user = AdminUsers::where('id','in',$id)->select()->toArray();
        foreach ($user as $value){
            if(!Role::isMyPid($value['role_id'])){
                return json_my_response('权限不足');
            }
        }
        foreach ($user as $value){
            if($value['portrait']){
                UpFile::delFile($value['portrait']);
            }
        }
        if(AdminUsers::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }
}